package org.penguin.study.bk.base.vo;

import lombok.Data;
import org.penguin.study.bk.constant.HttpStatus;

import java.io.Serializable;

/**
 * @author chengyinghua
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -2808073724222033898L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public Result() {
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = HttpStatus.OK;
        this.success = true;
        return this;
    }

    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(HttpStatus.OK);
        result.setMessage("成功");
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(HttpStatus.OK);
        result.setResult(data);
        return result;
    }

    public static <T> Result<T> ok(String msg, T data) {
        Result<T> result = new Result<T>();
        result.setSuccess(true);
        result.setCode(HttpStatus.OK);
        result.setMessage(msg);
        result.setResult(data);
        return result;
    }

    public static <T> Result<T> error(String msg, T data) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setCode(HttpStatus.Internal_Server_Error);
        result.setMessage(msg);
        result.setResult(data);
        return result;
    }

    public static Result<Object> error(String msg) {
        return error(HttpStatus.Internal_Server_Error, msg);
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> result = new Result<>();
        result.setCode(code);
        result.setMessage(msg);
        result.setSuccess(false);
        return result;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = HttpStatus.Internal_Server_Error;
        this.success = false;
        return this;
    }

    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noauth(String msg) {
        return error(HttpStatus.Not_Extended, msg);
    }

}